<?php
class Rebate_log_model extends BZ_Model
{
    protected $_table   = 'user_account_list';
    
    public function total($params)
    {
        $this->db->select('id');
        $this->db->from($this->_table);
        $this->db->where('uid', $params['uid']);
        if ( ! empty($params['id'])) {
            $this->db->where('id', $params['id']);
        }
        if (!empty($params['startData'])) {
            $this->db->where('date >=', strtotime($params['startData']. ' 00:00:00'));
        }
        if (!empty($params['endData'])) {
            $this->db->where('date <=', strtotime($params['endData'].' 23:59:59'));
        }
        if ( ! empty($params['flag']) ) {
            $this->db->where('flag', $params['flag']);
        }
        return $this->db->count_all_results();
    }
    
    public function page_list($page_num, $num, $params=array())
    {
        $this->db->select('id, o_income, deduction, r_income, name, contact, uid, n_amount_month, flag, date, n_amount_carry, LEFT(FROM_UNIXTIME(date), 7) AS month ', FALSE);
        $this->db->from($this->_table);
        $this->db->where('uid', $params['uid']);
        if ( ! empty($params['id'])) {
            $this->db->where('id', $params['id']);
        }
        if (!empty($params['startData'])) {
            $this->db->where('date >=', strtotime($params['startData']. ' 00:00:00'));
        }
        if (!empty($params['endData'])) {
            $this->db->where('date <=', strtotime($params['endData'].' 23:59:59'));
        }
        if ( ! empty($params['flag']) ) {
            $this->db->where('flag', $params['flag']);
        }
        $this->db->order_by('id', 'desc');
        $this->db->limit($page_num, $num);
        return $this->db->get();
    }
}